home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.08 Aug 89 / POOPDraw Code ƒ / !POOP EvtHandlers.c next >
Encoding:
C/C++ Source or Header  |  1989-06-13  |  6.4 KB  |  240 lines  |  [TEXT/KAHL]

  1. /********************************************************************/
  2. /*                                                                    */
  3. /*                        THE POOP SHELL                                */
  4. /*                                                                    */
  5. /********************************************************************/
  6. /*        An Adventure in Pseudo-Object-Oriented-Programming            */
  7. /********************************************************************/
  8. /*
  9. *   >>>    File name:        EventHandlers
  10. *
  11. *      >>>    Purpose:        The main event loop (Twiddle) and other high
  12.                             level event handlers
  13. *     >>>    Project:        Briefcase        
  14. *     >>>    Date:            April 5, 1989
  15. *      >>>    By:                Adam Treister
  16. *
  17. */
  18. /********************************************************************/
  19. /*    For Your Information            1802 Hillside Rd. SB CA 93101    */
  20. /********************************************************************/
  21. #include "PoopDrawInc"
  22.  
  23. void     DoMenuCommand    (long MenuSelectResult);
  24.  
  25. public void         Twiddle    (void);
  26. private    void         DoMouseDown        (EventRecord Event);
  27. private    void         DoMouseUp        (EventRecord Event);
  28. private    Boolean     ClickIsInActiveWindow    (WindowPtr ClickedWindow);
  29. private    void         DoKeyDown        (EventRecord Event);
  30. private    void         DoUpdate        (WindowPtr wP);
  31. private    void         DoActivate        (EventRecord Event);
  32. private    void        DoSuspendResume    (void);
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /*                                                                */
  37. /*                    MAIN EVENT LOOP                                */
  38. /*                                                                */
  39. /*                                                                */
  40. /* ------------------------------------------------------------ */
  41.  
  42. void Twiddle()
  43. {
  44.     extern    EventRecord Event;        
  45.     extern    Boolean MillerTime;
  46.             register long         Sleep = 10;
  47.             register Boolean    EventPending;
  48.             register WindowPtr     wP;
  49.  
  50.     while (!MillerTime)                    
  51.     {
  52.  
  53.         EventPending = WaitNextEvent(everyEvent, &Event,Sleep,NULL);
  54.         if (EventPending) 
  55.         { 
  56.             switch (Event.what)
  57.             {
  58.                 case mouseDown:        DoMouseDown(Event);            break;
  59.                 case mouseUp:        DoMouseUp(Event);            break;
  60.  
  61.                 case keyDown:
  62.                 case autoKey:        DoKeyDown(Event);            break;
  63.         
  64.                 case updateEvt:        DoUpdate((WindowPtr)Event.message);    break;
  65.         
  66.                 case activateEvt:    DoActivate(Event);            break;
  67.             }                
  68.             
  69.         }                /* if EventPending*/
  70.         else
  71.         {    
  72.                if (OurWindow(wP = MyFrontWindow()))    WDispatch(wP,IDLE,NULL);
  73.  
  74.                    
  75. }    }    }
  76.  
  77. Boolean                 DoubleClicked;            /* the last mouse down was a double */
  78. private    long            LastMouseUp;
  79. private    Point            LastMouseUpLoc;            /*not used for now */
  80.  
  81. /* ------------------------------------------------------------ */
  82. /*                                                                */
  83. /*                    DO MOUSE DOWN                                */
  84. /*                                                                */
  85. /*    This function handles a mouse down event. FindWindow is     */
  86. /*    called to find which window the mouse is in. Then the event */
  87. /*    is "Dispatched" there.                                        */
  88. /*                                                                */
  89. /* ------------------------------------------------------------ */
  90.  
  91. void DoMouseDown(Event)
  92. EventRecord     Event;    
  93. {
  94.     
  95.              WindowPtr        WhichWindow;        /* the event window */
  96.     register Point            MousePosition;        /* the current mouse position */
  97.     register int            Where;                /* the result of FindWindow */
  98.             Rect            DragRect;
  99.             long            okay = TRUE;
  100.             
  101.     DoubleClicked = (Event.when - LastMouseUp < GetDblTime());
  102.     
  103.     MousePosition = Event.where;
  104.     Where = FindWindow(MousePosition, &WhichWindow);
  105.  
  106.     switch (Where)
  107.     {
  108.         case inDesk:        break;    
  109.                     
  110.         case inMenuBar:        DoMenuCommand(MenuSelect(&Event.where));
  111.                             break;
  112.                     
  113.         case inSysWindow:    SystemClick(&Event, WhichWindow);
  114.                             break;
  115.                     
  116.         case inContent:        if (ClickIsInActiveWindow(WhichWindow))    
  117.                                 WDispatch(WhichWindow,MOUSEDOWN,NULL);    
  118.                             break;
  119.             
  120.         case inDrag:        DragRect = screenBits.bounds;
  121.                             DragWindow(WhichWindow,MousePosition, &DragRect);
  122.                             break;
  123.                     
  124.                     
  125.         case inGrow:        WDispatch(WhichWindow,GROW,NULL);    
  126.                             break;
  127.  
  128.         case inGoAway:        if (TrackGoAway(WhichWindow, MousePosition))
  129.                                 if ( _OptionKeyDown(Event))
  130.                                          while ((WhichWindow = MyFrontWindow()) AND okay)
  131.                                         {    WDispatch(WhichWindow,CLOSE,&okay);}
  132.                                  else        WDispatch(WhichWindow,CLOSE,&okay);
  133.  
  134.                              break;
  135.             default:
  136.             Oops("\pUnknown Window Type in MouseDown Handler", 0, TRUE);
  137.             break;
  138.                 
  139.     }    /* switch (FindWindow..)    */
  140. }
  141. /* ------------------------------------------------------------ */
  142. /*        DoMouseUp                                                */
  143. /*                                                                */
  144. /*    Set the current time into LastMouseUp for Double Click        */
  145. /*    Detection in DoMouseDown.                                    */
  146. /*                                                                */
  147. /* ------------------------------------------------------------ */
  148.  
  149. void DoMouseUp(Event)
  150. EventRecord     Event;
  151. {    
  152.     extern long LastMouseUp;
  153.      LastMouseUp = Event.when;
  154. }
  155.  
  156. /* ------------------------------------------------------------ */
  157. /*        Boolean ClickIsInActiveWindow(ClickedWindow)            */
  158. /*                                                                */
  159. /*    A quickie to select back windows if the're clicked.            */
  160. /*                                                                */
  161. /* ------------------------------------------------------------ */
  162. #define ItIsnt (!ItIs)
  163.  
  164. Boolean ClickIsInActiveWindow(ClickedWindow)
  165. WindowPtr ClickedWindow;
  166. {    
  167.     register Boolean        ItIs;
  168.     long type;
  169.         
  170.     ItIs = (ClickedWindow == FrontWindow());
  171.     if (ItIsnt)    SelectWindow(ClickedWindow);
  172.     return(ItIs);
  173. }
  174.         
  175. /* ------------------------------------------------------------ */
  176. /*                                                                */
  177. /*                    DO KEY DOWN                                    */
  178. /*                                                                */
  179. /*    This function handles a keydown event.  If the command        */
  180. /*    key is down its a menu command, otherwise dispatch it        */
  181. /*                                                                */
  182. /*                                                                */
  183. /* ------------------------------------------------------------ */
  184.  
  185. void DoKeyDown(Event)
  186. EventRecord     Event;
  187. {
  188.     register char                key;
  189.             
  190.     key = Event.message & charCodeMask;
  191.         
  192.     if (_CmdKeyDown(Event))            DoMenuCommand(MenuKey(key));
  193.     else if (MyFrontWindow())        WDispatch(MyFrontWindow(),KEYDOWN,NULL);
  194. }
  195.  
  196. /* ------------------------------------------------------------ */
  197. /*                                                                */
  198. /*                    DO UPDATE                                    */
  199. /*                                                                */
  200. /* ------------------------------------------------------------ */
  201.  
  202. void DoUpdate(wP)
  203. register WindowPtr    wP;
  204. {    
  205.         GrafPtr                PortSave;    
  206.     
  207.     GetPort(&PortSave);
  208.     SetPort(wP);
  209.     
  210.     BeginUpdate(wP);
  211.         WDispatch(wP,UPDATE,NULL);
  212.     EndUpdate(wP);
  213.  
  214.     SetPort(PortSave);
  215. }
  216.  
  217. /* ------------------------------------------------------------ */
  218. /*                                                                */
  219. /*                    DO ACTIVATE                                    */
  220. /*                                                                */
  221. /* ------------------------------------------------------------ */
  222.  
  223. void DoActivate(Event)
  224. EventRecord     Event;
  225. {
  226.     register WindowPtr        ActivateWindow;            
  227.     register int            Message = DEACTIVATE;
  228.         
  229.     TurnArrowOn();
  230.     ActivateWindow = (WindowPtr) Event.message;
  231.     
  232.     if (Event.modifiers & activeFlag)            /* Activate vs Deactivate  */
  233.     {    
  234.         SetPort(ActivateWindow);
  235.         Message = ACTIVATE;
  236.     }    
  237.     WDispatch(ActivateWindow,Message,NULL);
  238. }
  239.  
  240.